package com.tcc.fastline.DAO;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;
import com.tcc.fastline.DAO.interfaces.IUsuarioDAO;
import com.tcc.fastline.Domain.Fila;
import com.tcc.fastline.Domain.Usuario;
import com.tcc.fastline.Exceptions.ValidationServerException;
import com.tcc.fastline.HttpMethods.ExecuteTask;
import com.tcc.fastline.HttpMethods.HttpDeleteTask;
import com.tcc.fastline.HttpMethods.HttpGetTask;
import com.tcc.fastline.HttpMethods.HttpPostTask;
import com.tcc.fastline.HttpMethods.HttpPutTask;
import com.tcc.fastline.HttpMethods.Urls;

public class UsuarioDAO implements IUsuarioDAO {

	private Context contexto;
	private Gson gson;
	
	public UsuarioDAO(Context context) {
		this.contexto = context;
		this.gson = new Gson();
	}

	@Override
	public Usuario CriarUsuario(Usuario user) throws ValidationServerException, Exception{
		try {
			String url = Urls.URL_USUARIOS;
			
			String usuarioJSON = user.toString();
			
			String retorno = ExecuteTask.Execute(new HttpPostTask(url, usuarioJSON), contexto);
			
			return new Usuario(retorno);
			
		}catch (ValidationServerException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Usuario> BuscarUsuarios() throws ValidationServerException, Exception{
		List<Usuario> usuarios = new ArrayList<Usuario>();		
		try {
			String url = Urls.URL_USUARIOS;
			
			//AsyncTask<Object, Void, String> task = new HttpGetTask( url).execute();
			//String retorno = task.get();
			String retorno = ExecuteTask.Execute(new HttpGetTask(url), contexto);

			usuarios = gson.fromJson(retorno, new com.google.gson.reflect.TypeToken<List<Usuario>>(){}.getType());
			
		} catch (ValidationServerException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}
		
		return usuarios;
	}

	@Override
	public void ExcluirUsuario(Integer codigoUsuario) throws ValidationServerException, Exception{
		try {
			String url = Urls.URL_USUARIOS + codigoUsuario.toString();
			
			//AsyncTask<Object, Void, String> task = new HttpDeleteTask(url).execute();
			String retorno = ExecuteTask.Execute(new HttpDeleteTask(url), contexto);
			
		}catch (ValidationServerException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}
	}

	@Override
	public void AtualizarUsuario(Usuario user) throws ValidationServerException, Exception{
		try {
			String url = Urls.URL_USUARIOS;
			
			String usuarioJSON = user.toString();
			
			//AsyncTask<Object, Void, String> task = new HttpPutTask(url, usuarioJSON).execute();
			String retorno = ExecuteTask.Execute(new HttpPutTask(url, usuarioJSON), contexto);
			
		}catch (ValidationServerException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}
	}

}
